package com.skywaregroup.arenal;

import java.io.Serializable;

import javax.persistence.*;
import javax.xml.bind.annotation.*;

/**
 * Represents municipality (obshtina) as an administrative unit.
 * @author Administrator
 */
@XmlRootElement(name="Municipality")
@XmlAccessorType(XmlAccessType.NONE) 
@Entity 
@Table(name="municipalities")
public class Municipality implements Serializable {
	
	private static final long serialVersionUID = 1L;

	/**
	 * Region object.
	 */
	@ManyToOne(targetEntity=Region.class, fetch=FetchType.LAZY)
	@JoinColumn(name="region_adm_code", nullable=false, updatable=false, insertable=true)
	public Region region;
	
	/**
	 * Foreign key to regions.
	 */
	@Id
	@Column(name="region_adm_code", nullable=false, updatable=false, insertable=true)
	public Integer regionAdmCode;
	
	/**
	 * Administrative identifier.
	 * Used by Ministry of Health for practice codes (second two letters of the registration code). 
	 */
	@Id
	@Column(name="adm_code", nullable=false)
	public Integer admCode;
	
	/**
	 * Used by Ministry of Health for coding health regions.
	 */
	@Column(name="hr_code", nullable=false)
	public Integer hrCode;
	
	@XmlElement(name="Code")
	public String getCode() {
		return String.format("%02d", this.admCode);
	}
	
	public void setCode(String s){
		if (!s.matches("^\\d{2}$")) {throw new IllegalArgumentException("Code must be 2-digits string.");}
		this.admCode = Integer.parseInt(s);
	}
	
	@XmlElement(name="HRCode")
	public String getHRCode() {
		return String.format("%02d", this.hrCode);
	}
	
	public void setHRCode(String s){
		if (!s.matches("^\\d{2}$")) {throw new IllegalArgumentException("Code must be 2-digits string.");}
		this.hrCode = Integer.parseInt(s);
	}
	
	/**
	 * Name of municipality.
	 */
	@Column(name="name", nullable=false, length=50) 
	@XmlElement(name="Name")
	public String name;
	
	public Municipality() {}

}
